//
//  JMMyFeedbackController.m
//  NewiOSProject
//
//  Created by li on 2023/9/13.
//  Copyright © 2023 范智渊. All rights reserved.
//

#import "JMMyFeedbackController.h"
#import "LayoutView.h"

#import "JMPickerSelectionView.h"
#import "JMPickerSelectionModel.h"
#import "PaddingView.h"
#import "LayoutScrollView.h"
#import "CustomStackView.h"
#import "CustomStackView.h"
#import "LayoutView.h"
#import "UIView+PopupProtocol.h"
#import "JMAuthentiactionConfirmView.h"

@interface JMMyFeedbackController ()<UITextViewDelegate>
@property(nonatomic, strong) UIStackView *rootStack;
@property(nonatomic, strong) LayoutScrollView *scrollView;
@property(nonatomic, strong) JMAuthentiactionConfirmView *confirmView;

@property(nonatomic, strong) UILabel *countLab;
@property(nonatomic, strong) UILabel *placeholder;
@property(nonatomic, strong) UITextView *textView;
@property(nonatomic, assign) NSInteger maxInput;


@end

@implementation JMMyFeedbackController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.TitleLabel.text = @"Retroalimentación";
    self.maxInput = 160;
    [self setupViews];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
}


- (void)setupViews {
    
    [self.view addSubview:self.rootStack];
    [self.rootStack addArrangedSubview:self.scrollView];
    self.scrollView.rootStack.contentView.spacing = 0;
    

    CustomStackView *v_stack = [CustomStackView init:(UILayoutConstraintAxisVertical)];

    [self.scrollView.rootStack addContentView:[[PaddingView initWithContentView:v_stack edgeInsets:(UIEdgeInsetsMake(0, 0, 0, 0))] then:^(PaddingView * _Nonnull view) {
        [view.widthAnchor constraintEqualToConstant:kScreenWidth].active = YES;
        [view.heightAnchor constraintEqualToConstant:48].active = YES;
    }]];
    
    UILabel *title = [UILabel new];
    title.font = [UIFont systemFontOfSize:14 weight:(UIFontWeightRegular)];
    title.numberOfLines = 0;
    title.textColor = [UIColor colorWithHexString:@"#222222"];
    title.text = @"Describe el problema relacionado encontrado";
    [v_stack addArrangedSubview:[PaddingView initWithContentView:title edgeInsets:(UIEdgeInsetsMake(0, 24, 0, 24))]];
    
    CustomStackView *v_input_stack = [CustomStackView init:(UILayoutConstraintAxisVertical)];
    v_input_stack.backgroundColor = Hex_Str_COLOR(@"#F6F9FE");
    v_input_stack.spacing = 10;
    [self.scrollView.rootStack addContentView:[[PaddingView initWithContentView:v_input_stack edgeInsets:(UIEdgeInsetsMake(0, 0, 0, 0))] then:^(PaddingView * _Nonnull view) {
        [view.widthAnchor constraintEqualToConstant:kScreenWidth].active = YES;
        [view.heightAnchor constraintEqualToConstant:200].active = YES;
    }]];
    
    UITextView *textView = [UITextView new];
    self.textView = textView;
    textView.textColor = [UIColor colorWithHexString:@"#0F1511"];
    textView.backgroundColor = UIColor.clearColor;
    textView.font = [UIFont systemFontOfSize:14];
    [textView.heightAnchor constraintEqualToConstant:140].active = YES;
    textView.delegate = self;
    [v_input_stack addArrangedSubview:[PaddingView initWithContentView:textView edgeInsets:(UIEdgeInsetsMake(12, 24, 0, 24))]];
    
    UILabel *placeholder = [UILabel new];
    placeholder.font = [UIFont systemFontOfSize:14];
    placeholder.textColor = [UIColor colorWithHexString:@"#999999"];
    placeholder.text = @"Por favor califique esta aplicación (opcional)";
    [placeholder sizeToFit];
    placeholder.translatesAutoresizingMaskIntoConstraints = NO;
    self.placeholder = placeholder;
    placeholder.numberOfLines = 0;
    [textView addSubview:placeholder];
    [NSLayoutConstraint activateConstraints:@[
        [placeholder.topAnchor constraintEqualToAnchor:textView.topAnchor constant:6.5],
        [placeholder.leftAnchor constraintEqualToAnchor:textView.leftAnchor constant:4.5],
        [placeholder.rightAnchor constraintEqualToAnchor:textView.rightAnchor]
    ]];
    
    UILabel *count = [UILabel new];
    count.font = [UIFont systemFontOfSize:14];
    count.textColor = [UIColor colorWithHexString:@"#9B9B9B"];
    count.text = [NSString stringWithFormat:@"0/%ld", self.maxInput];
    count.textAlignment = NSTextAlignmentRight;
    [v_input_stack addArrangedSubview:count];
    self.countLab = count;
    
    [self.rootStack addArrangedSubview: [PaddingView initWithContentView:self.confirmView edgeInsets:(UIEdgeInsetsMake(20, 0, 64, 24))]];
}

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    
    NSString *text = textView.text;
    if (text.length > self.maxInput) {
        text = [text substringToIndex:self.maxInput];
    }
    textView.text = text;
    
    self.countLab.text= [NSString stringWithFormat:@"%ld/%ld", text.length, self.maxInput];
    
    
    self.placeholder.hidden = text.length > 0 ? YES : NO;
    self.confirmView.confirmBut.enable = text.length > 0 ? YES : NO;
    
}

#pragma mark - 发送
- (void)send:(UIButton *)sender {
    
//    [self.view showLoading:@"" animated:YES];
    // [self startAnimating:nil];
    NSDictionary *para = @{
        @"title" : @"native_feedback",
        @"content": self.textView.text
    };
    [[JMBaseRequest sharedInstance] requestBasePost:BasePostRequestUrl(@"/api/user/complaint") params:para success:^(NSDictionary * _Nullable dataDic, NSArray * _Nullable dataArray) {
        [self stopAnimating];
        [self showSuccess:@"Enviado con éxito"];
        
        dispatch_async(dispatch_get_main_queue(), ^{
//         
            [self.navigationController popToRootViewControllerAnimated:YES];
        });
        } fail:^(NSString * _Nonnull errorMsg) {

        }];
}

#pragma mark - 懒加载
- (UIStackView *)rootStack {
    if (!_rootStack) {
        self.rootStack = [UIStackView new];
        _rootStack.alignment = UIStackViewAlignmentCenter;
        _rootStack.axis = UILayoutConstraintAxisVertical;
        _rootStack.translatesAutoresizingMaskIntoConstraints = NO;
        [self.view addSubview:_rootStack];
        [self.rootStack mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.equalTo(@(NAVIGATION_BAR_HEIGHT()));
        }];
    }
    return  _rootStack;
}
- (LayoutScrollView *)scrollView {
    if (!_scrollView) {
        self.scrollView = [LayoutScrollView new];
        _scrollView.rootStack.contentView.axis = UILayoutConstraintAxisVertical;
        [_scrollView.widthAnchor constraintEqualToConstant: kScreenWidth].active = YES;
        _scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.rootStack.layoutViewAlignment = LayoutViewAlignmentTop;
    }
    return _scrollView;
}

- (JMAuthentiactionConfirmView *)confirmView {
    if (!_confirmView) {
        self.confirmView = [JMAuthentiactionConfirmView new];
        _confirmView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [_confirmView.widthAnchor constraintEqualToConstant:kScreenWidth - 48].active = YES;
        [_confirmView.confirmBut addTarget:self action:@selector(send:) forControlEvents:(UIControlEventTouchUpInside)];
        _confirmView.confirmBut.enable = NO;
    }
    return _confirmView;
}

@end
